package com.siqi.hr.filter;

import com.siqi.hr.system.organization.entity.Employee;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MainFilter implements Filter {
	private String encoding;

	public void destroy() {
		this.encoding = null;
	}

    /**
	 * 过滤没有登录直接访问目标地址xiaogq
	 * @param servletRequest
	 * @param servletResponse
	 * @param filterChain
	 * @throws IOException
	 * @throws ServletException
     */
	public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		response.setContentType("charset=" + this.encoding);
		response.setCharacterEncoding(this.encoding);
		HttpSession session = request.getSession();
		String servletPath = request.getServletPath();
		//验证session是否过期xiaogq
		if ((!"/Employee/queryLogin.do".equals(servletPath))
			&& (!servletPath.startsWith("/dingtalk")) && (!servletPath.startsWith("/Msm/Employee")) && (!servletPath.startsWith("/Msm/BoleRecom"))
			&& (!servletPath.startsWith("/OSS/uploadFiles")) && (!servletPath.startsWith("/OSS/fileUploadAndSave"))
			&& (!servletPath.startsWith("/uedit/themes/default/images/loading.gif")) && (!servletPath.startsWith("/uedit/themes/default/images/spacer.gif"))
//			&& (!servletPath.startsWith("/OrgManage/synchroDept")) && (!servletPath.startsWith("/OrgManage/synchroEmp"))
			&& (session == null || session.getAttribute("emp") == null)) {
			response.sendRedirect(request.getContextPath() + "/login.jsp");
		} else {
			SysContent.setEmployee((Employee) request.getSession().getAttribute("emp"));
			filterChain.doFilter(request, response);
		}
	}

	public void init(FilterConfig config) throws ServletException {
		this.encoding = config.getInitParameter("encoding");
		if ((this.encoding == null) || ("".equals(this.encoding)))
			throw new RuntimeException("过滤器没有配置请求参数格式");
	}
}
